<template>
    <div class="app-container">
        <el-card shadow="never" v-loading="loading">
            <el-row :gutter="5">
                <el-col :span="24" class="main-card-view">
                    <el-row v-if="userInfo.roleName === '运营人员' || userInfo.roleName === '管理员'">
                        <el-card shadow="never" class="top-search-view-card">
                            <el-row>
                                <el-form :inline="true" :model="searchForm" class="demo-form-inline search-view">
                                    <el-form-item>
                                       <el-button type="primary" @click="showCreateDialog" class="new-button">添加</el-button>
                                    </el-form-item>
                                </el-form>
                            </el-row>
                        </el-card>
                    </el-row>

                    <el-row>
                        <el-card shadow="never">
                            <el-table
                                :data="tableData"
                                :stripe="true"
                                style="width: 100%"
                                row-key="uid"
                                :tree-props="{children: 'childs',}">

                                <el-table-column prop="wxName" align="center" label="名称"/>
                                <el-table-column prop="createTime" align="center" label="创建时间"/>
                                <el-table-column prop="fileName" align="center" label="图片">
                                    <template slot-scope="scope">
                                        <el-image :src="scope.row.fileName" style="width: 50px;height: 50px;"/>
                                    </template>
                                </el-table-column>
                                <el-table-column align="center" label="操作" width="200">
                                    <template slot-scope="scope">
                                      <el-button type="text" class="table-edit-btn"
                                                   @click="showEditDialog(scope.$index, scope.row)"></el-button><!--编辑-->
                                        <el-button type="text" class="table-delete-btn"
                                                   @click="handleDelete(scope.$index, scope.row)"></el-button><!--删除-->
                                    </template>
                                </el-table-column>
                            </el-table>
                            <XPagination ref="pageInfo" :total="total" :get-data="getList"/>
                        </el-card>
                    </el-row>
                </el-col>
            </el-row>
        </el-card>

        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
            <el-form label-width="120px" ref="form" :model="form" :rules="formRules">
                <el-form-item label="名称" prop="wxName">
                    <el-input v-model="form.wxName"/>
                </el-form-item>
                <el-form-item label="图片" prop="fileName">
                    <x-upload v-model="form.fileName"/>
                </el-form-item>
            </el-form>
            <template slot="footer">
                <el-button type="primary" @click="handleSave">保存</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import XPagination from "../../components/xpagination/XPagination";
    import * as newMedia from "../../api/newMedia"
    import * as formUtils from "../../utils/formUtils";
    import XUpload from "../../components/xupload/XUpload";
    export default {
        name: "list",
        components: {XUpload, XPagination},
        data() {
            return {
                tableData: [],
                loading: false,
                dialogVisible:false,
                userInfo:{},
                total:0,
                dialogTitle:"添加新媒体",
                columnGradeList:['一级','二级','三级'],
                form:{
                    uid:"",
                    wxName:"",
                    fileName:"",
                },
                editStatus:"add",
                formRules:{
                    wxName: [
                        { required: true, message: '不能为空', trigger: 'blur' },
                    ]
                },

            }
        },
        methods: {
            getList(pageInfo) {
                const param = {
                    ...pageInfo
                }
                param.roleUid=this.userInfo.roleUid
                newMedia.queryByPage(param).then(response => {
                    console.log(response)
                    this.tableData = response.data.data.rows
                    this.total = response.data.data.total
                    this.loading = false
                }).catch(error => {
                    console.log(error)
                })
            },
            handleDelete(index, data) {
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    const param = {
                        uid: data.uid,
                    }
                    this.loading = true
                    newMedia.deleteById(param).then(result => {
                        this.loading = false
                        this.$message.success(result.data.msg)
                        this.handleSearch()
                    }).catch(error => {
                        this.loading = false
                        console.log(error)
                    })
                }).catch(() => {
                    console.log('取消删除')
                });
            },
            handleSearch() {
                this.getList()
            },
            showCreateDialog() {
                this.dialogVisible = true;
                this.$nextTick(() => {
                    this.dialogTitle = "添加新媒体"
                    this.editStatus = "add"
                    this.$refs.form.resetFields()
                    this.form = Object.assign({}, this.$options.data().form)
                })
            },
            handleSave() {
                if (this.editStatus === "add") {
                    this.handleAdd()
                } else {
                    this.handleEdit()
                }
            },
            handleAdd() {
                this.$refs["form"].validate((valid) => {
                    if (!valid) {
                        return false
                    }
                    const param = Object.assign({}, this.form)
                    formUtils.filterFormFields(param)
                    newMedia.insert(param).then(result => {
                        this.$message.success(result.data.msg)
                        this.dialogVisible = false
                        this.handleSearch()
                    }).catch(error => {
                        this.$message.error(error)
                        console.log(error)
                    })

                })
            },
            handleEdit() {
                this.$refs["form"].validate((valid) => {
                    if (!valid) {
                        return false
                    }
                    const param = Object.assign({}, this.form)
                    formUtils.filterFormFields(param)
                    newMedia.update(param).then(result => {
                        this.$message.success(result.data.msg)
                        this.dialogVisible = false
                        this.handleSearch()
                    }).catch(error => {
                        this.$message.error(error)
                        console.log(error)
                    })
                })
            },
            showEditDialog(index, data) {
                this.dialogVisible = true
                this.$nextTick(() => {
                    this.$refs.form.resetFields()
                    this.form = Object.assign({}, data)
                    this.dialogTitle = "编辑新媒体"
                    this.editStatus = "edit"
                })
            },
        },
        mounted() {
            this.userInfo = JSON.parse(localStorage.getItem('user_info'))
            this.handleSearch()
        }
    }
</script>

<style scoped lang="scss">

</style>
